//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package rtk.olinkstar;

import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.os.StatFs;

import java.io.File;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.CopyOnWriteArrayList;

import rtk.BleManager;
import rtk.GattCallback;

public class RtkService implements GattCallback {
    public double sdkAccuracy = -1.0D;
    public LocationDataManager locDataManager;
    protected static String currentTime = "";

    protected static boolean flagGPGGA = false;
    protected static boolean flagXXRMC = false;
    protected static boolean[] hasGSV = new boolean[1];
    public NmeaAnalyzer nmeaAnalyzer;
    protected static String nmeaGPGGA = "";
    protected static boolean flagXXGSV = false;
    boolean bRtcmInitAckRec;
    private boolean isHead;
    private boolean isReturnGot;
    private final String PACK_HEAD;
    private final String PACK_FLAG;
    public final int BLE_RCV_STATE_IDLE = 0;
    public final int BLE_RCV_STATE_BUSY = 1;
    int ble_rcv_state;
    public static BleManager bleManager;
    CPktStatistic m_nmeaCPktStatistic;
    CPktStatistic m_RtcmCPktStatistic;
    private final String BLETX_BUFFER = "BLETXBUFFER";
    private final String BLETX_SPEED = "BLETX_SPEED";
    private final String RTCM_BUFFER = "RTCMBUFFER";
    private final String BLETX_BUFFER_RT = "BLETXBUFFERnew";
    private final String BLETX_BUFFER_ACK = "BLETX_BUFFER_ACK";
    private final int BUUFER_LEN_MAX = 4096;
    private ArrayList<byte[]> m_ListBLETxBuffer;
    int gl_ListBLETxBufferRead;
    int gl_ListBLETxBufferWrite;
    private BLETxDataThread mBLETxDataThread;
    private ArrayList<byte[]> m_ListBLETxBufferAck;
    int gl_ListBLETxBufferAckRead;
    int gl_ListBLETxBufferAckWrite;

    private RTCMThread mRTCMThread;
    public int m_nBleRssi;

    private int packReturn;
    private byte[] repairIndex;
    boolean bNmeaTxRunnable;
    private ACKTxThread mACKTxThread;
    private NmeaTxThread mNmeaTxThread;

    final boolean GET_AckTxRequest = true;
    final boolean SET_AckTxRequest = false;
    private final String ACK_BUFFER = "ACKBUFFER";
    private final String ACK_DataBUFFER = "ACK_DataBUFFER";
    boolean gl_bAckTxRequest;
    boolean gl_bAckDataTxRequest;
    boolean gl_bAckAckRec;
    int gl_nPktTmpCnt;
    boolean gl_InitPktFirst;
    private int gl_nPktCntBack;
    boolean gl_bDataRec;
    boolean gl_bOnreadpkt;
    int nRepert;
    private boolean bisLastPkt;
    long gl_RecPrePktTime;
    private int gl_nPktReadCnt;
    private int gl_nPktCnt;
    int gl_nDeInitPktCnt;

    public final long count_Rev_Data_time_max;
    int nReTxCnt;
    byte[] reTxN;
    private Timer timerVERSION;
    private TimerTask taskVERSION;

    public boolean firstConnect;

    private boolean flagGNVER;
    private CopyOnWriteArrayList<byte[]> ListNmeaBack;
    private NmeaThread mNmeaThread;


    private StringBuilder para;
    private boolean flag_para_start;
    private Timer timerPCMDT;
    private boolean flagPCMDT;
    public int devicePower;
    private final String NMEA_BUFFER = "NMEABUFFER";
    private CopyOnWriteArrayList<CopyOnWriteArrayList<byte[]>> ListNmeaBackList;
    private CopyOnWriteArrayList<CopyOnWriteArrayList<byte[]>> ListBackList;
    private CopyOnWriteArrayList<byte[]> sRtcmPktBuffer;

    public String bluetoothName;
    public String BoardVersion;
    public String BootloaderVersion;
    public String BGIVersion;
    public String BGIDateTime;

    private OnNmeaListener onNmeaListener;

    public double getSdkAccuracy() {
        return this.sdkAccuracy;
    }

    public int getBleRssi() {
        return this.m_nBleRssi;
    }

    public int getDevicePower() {
        return this.devicePower;
    }

    public RtkService(BleManager bleManager, OnNmeaListener onNmeaListener) {
        this.bleManager = bleManager;
        this.onNmeaListener = onNmeaListener;
        this.locDataManager = LocationDataManager.INSTANSE;
        this.nmeaAnalyzer = new NmeaAnalyzer(1);
        this.PACK_HEAD = "head";
        this.PACK_FLAG = "pack";
        this.m_nmeaCPktStatistic = new CPktStatistic();
        this.m_RtcmCPktStatistic = new CPktStatistic();

        this.m_ListBLETxBufferAck = null;

        this.packReturn = -1;
        this.bNmeaTxRunnable = true;

        this.gl_InitPktFirst = true;
        this.gl_bDataRec = true;
        this.count_Rev_Data_time_max = 3000L;
        this.firstConnect = true;

        this.ListBackList = new CopyOnWriteArrayList();

        this.m_nmeaCPktStatistic.SetFileNmae("PktStatistic_LOG.txt");
        this.m_RtcmCPktStatistic.SetFileNmae("PktStatistic_LOG.txt");
        this.m_nmeaCPktStatistic.ClearStatistic();
        this.m_RtcmCPktStatistic.ClearStatistic();

        this.timerVERSION = new Timer();
        this.taskVERSION = new TimerTask() {
            public void run() {
                try {
                    if (!RtkService.this.flagGNVER) {
                        try {
                            byte[] e = "$PCMDV\r\n".getBytes("UTF-8");
                            if (bleManager != null)
                                bleManager.write(e);
                        } catch (UnsupportedEncodingException var3) {
                            var3.printStackTrace();
                        }
                    } else if (RtkService.this.flagGNVER) {
                        RtkService.this.timerVERSION.cancel();
                    }
                } catch (Exception var4) {
                    var4.printStackTrace();
                }

            }
        };
        this.timerVERSION.schedule(this.taskVERSION, 0L, 10000L);
    }


    @Override
    public void onDisconnected() {
        finalize();
    }

    @Override
    public void onConnectionError() {

    }

    @Override
    public void onTimeOut() {
        setRtcmThread(false);
    }

    @Override
    public void onConnected(BluetoothDevice device) {

        if (timerPCMDT != null) {
            timerPCMDT.cancel();
            timerPCMDT = null;
        }
        timerPCMDT = new Timer();
        TimerTask taskPCMDT = new TimerTask() {
            public void run() {
                try {
                    byte[] sw1 = getPCMDT().getBytes("UTF-8");
                    RtkService.this.TxDifDataV2(sw1, sw1.length);
                    if (flagPCMDT) {
                        timerPCMDT.cancel();
                    }

                } catch (Exception var5) {
                    timerPCMDT.cancel();
                }

            }
        };
        timerPCMDT.schedule(taskPCMDT, 0L, 1000L);

        if (mNmeaTxThread != null)
            mNmeaTxThread.interrupt();
        mNmeaTxThread = new NmeaTxThread();
        mNmeaTxThread.start();

        if (mNmeaThread != null)
            mNmeaThread.interrupt();
        mNmeaThread = new NmeaThread();
        mNmeaThread.start();

        if (mBLETxDataThread != null)
            mBLETxDataThread.interrupt();
        mBLETxDataThread = new BLETxDataThread();
        mBLETxDataThread.start();

        if (mACKTxThread != null)
            mACKTxThread.interrupt();
        mACKTxThread = new ACKTxThread();
        mACKTxThread.start();

    }


    @Override
    public void onRead(byte[] buffer) {

        byte[] var22;
        if (buffer[0] == 126) {
            if (buffer[2] == 7) {
                RtkService.this.devicePower = (new Byte(buffer[3])).intValue();
            } else {
                if (buffer[2] == 1) {
                    if (RtkService.this.ListNmeaBack != null && RtkService.this.ListNmeaBack.size() > 0) {
                        if (RtkService.this.gl_nPktReadCnt < RtkService.this.gl_nPktCnt) {
                            if (RtkService.this.gl_nDeInitPktCnt < 3) {
                                ++RtkService.this.gl_nDeInitPktCnt;
                                return;
                            }

                            CopyOnWriteArrayList ss = RtkService.this.ListNmeaBack;
                            RtkService.this.DealNmeaListData(ss, true);
                            RtkService.this.AckTxDataRequest(false, true);
                        } else {
                            RtkService.this.gl_nDeInitPktCnt = 0;
                        }
                    }

                    RtkService.this.gl_bAckAckRec = true;
                    synchronized (BLETX_SPEED) {
                        RtkService.this.ble_rcv_state = 1;
                    }


                    ++RtkService.this.gl_nPktTmpCnt;
                    if (!RtkService.this.gl_bDataRec) {
                        --RtkService.this.gl_nPktTmpCnt;
                    }

                    if (!RtkService.this.gl_InitPktFirst) {
                        if (RtkService.this.gl_bDataRec) {
                            RtkService.this.m_nmeaCPktStatistic.SetTime(RtkService.this.gl_RecPrePktTime);
                            RtkService.this.m_nmeaCPktStatistic.CNTINIPKT();
                            RtkService.this.m_nmeaCPktStatistic.CNTWillPKT(RtkService.this.gl_nPktCntBack);
                            RtkService.this.m_nmeaCPktStatistic.SaveStatistic(0);
                        }
                    } else {
                        RtkService.this.m_nmeaCPktStatistic.ClearStatistic();
                    }

                    RtkService.this.gl_bDataRec = false;
                    byte sRet = 0;
                    if (sRet >= RtkService.this.gl_nPktCnt) {
                        boolean var30 = false;
                    }

                    RtkService.this.gl_nPktCnt = buffer[3] & 255;
                    RtkService.this.gl_nPktCntBack = RtkService.this.gl_nPktCnt;
                    byte[] time2 = new byte[]{126, 4, 6, 122};
                    RtkService.this.gl_nPktReadCnt = 0;
                    RtkService.this.bisLastPkt = false;
                    RtkService.this.gl_RecPrePktTime = System.currentTimeMillis();
                    RtkService.this.gl_bOnreadpkt = false;
                    RtkService.this.nRepert = 0;
                    if (RtkService.this.ListNmeaBack == null) {
                        RtkService.this.ListNmeaBack = new CopyOnWriteArrayList();
                    }

                    if (RtkService.this.ListNmeaBack.size() > 0) {
                        RtkService.this.ListNmeaBack.clear();
                    }

                    RtkService.this.BLETxAck(time2);
                } else if (buffer[2] == 6) {
                    RtkService.this.isHead = true;
                    RtkService.this.m_RtcmCPktStatistic.CNTINIPKTACK();
                    RtkService.this.isReturnGot = true;
                    RtkService.this.bRtcmInitAckRec = false;
                    synchronized (PACK_HEAD) {
                        PACK_HEAD.notifyAll();
                    }
                } else if (buffer[2] == 8) {
                    RtkService.this.m_RtcmCPktStatistic.CNTDATAPKTSACK();
                    RtkService.this.packReturn = 0;
                    RtkService.this.isReturnGot = true;
                    var22 = new byte[]{126, 4, 9, 122};
                    RtkService.this.BLETxAck(var22);
                    RtkService.this.m_RtcmCPktStatistic.CNTDATAACKACK();
                    synchronized (PACK_FLAG) {
                        PACK_FLAG.notifyAll();
                    }

                } else if (buffer[2] == 9) {
                    RtkService.this.m_nmeaCPktStatistic.CNTDATAACKACK();
                    RtkService.this.gl_bAckAckRec = true;
                } else if (buffer[2] != 4 && buffer[2] == 2) {
                    if (buffer[3] == 255) {
                        RtkService.this.packReturn = 2;
                    } else {
                        RtkService.this.packReturn = 1;
                        int var23 = buffer[3] & 255;
                        RtkService.this.m_RtcmCPktStatistic.SetRETXPKT(var23);
                        RtkService.this.m_RtcmCPktStatistic.CNTRETXCNT();
                        RtkService.this.repairIndex = new byte[var23];
                        System.arraycopy(buffer, 4, RtkService.this.repairIndex, 0, var23);
                        RtkService.this.isReturnGot = true;
                    }

                    synchronized (PACK_FLAG) {
                        PACK_FLAG.notifyAll();
                    }
                }
            }
        } else {
            if (RtkService.this.firstConnect) {
                try {
                    var22 = "$PCMDN,1,0\r\n".getBytes("UTF-8");
                    RtkService.this.BLETxAck(var22);
                } catch (Exception var14) {
                    var14.printStackTrace();
                    StringWriter var21 = new StringWriter();
                    var14.printStackTrace(new PrintWriter(var21, true));
                }

                RtkService.this.firstConnect = false;
            }

            RtkService.this.gl_bDataRec = true;
            var22 = new byte[buffer.length - 1];
            System.arraycopy(buffer, 1, var22, 0, buffer.length - 1);
            if (RtkService.this.gl_nPktCnt != 0 && RtkService.this.gl_nPktReadCnt < RtkService.this.gl_nPktCnt) {
                RtkService.this.gl_InitPktFirst = false;
                int var27 = buffer[0] & 255;
                if (RtkService.this.GetIndexBuffer(RtkService.this.ListNmeaBack, var27) != null) {
                    return;
                }

                RtkService.this.m_nmeaCPktStatistic.CNTSetPKT();
                RtkService.this.ListNmeaBack.add(buffer);
                if (RtkService.this.gl_bOnreadpkt) {
                    if (RtkService.this.reTxN[RtkService.this.nReTxCnt - 1] == var27) {
                        RtkService.this.bisLastPkt = true;
                        RtkService.this.gl_bOnreadpkt = false;
                        RtkService.this.nRepert = 0;
                    } else {
                        RtkService.this.bisLastPkt = false;
                    }
                } else if (var27 == RtkService.this.gl_nPktCnt - 1) {
                    RtkService.this.bisLastPkt = true;
                } else {
                    RtkService.this.bisLastPkt = false;
                }

                RtkService.this.gl_RecPrePktTime = System.currentTimeMillis();
                RtkService.this.gl_nPktReadCnt = RtkService.this.gl_nPktReadCnt + 1;
            }

            if (RtkService.this.gl_nPktCnt != 0 && RtkService.this.gl_nPktReadCnt == RtkService.this.gl_nPktCnt) {
                RtkService.this.m_nmeaCPktStatistic.CNTPerPKTS();
                RtkService.this.bisLastPkt = false;
                RtkService.this.gl_bOnreadpkt = false;
                RtkService.this.nRepert = 0;
                CopyOnWriteArrayList var25 = RtkService.this.ListNmeaBack;
                RtkService.this.DealNmeaListData(var25, true);
                RtkService.this.AckTxDataRequest(false, true);
                synchronized (BLETX_SPEED) {
                    RtkService.this.ble_rcv_state = 0;
                }

                RtkService.this.gl_nPktReadCnt = 0;
                RtkService.this.gl_nPktCnt = 0;
            }
        }

    }

    @Override
    public void onWrite(boolean var1) {

    }

    @Override
    public void onRSSI(int rssi, int status) {
        RtkService.this.m_nBleRssi = rssi;
        RtkService.this.m_nmeaCPktStatistic.SetBleRssi(RtkService.this.m_nBleRssi);
        LocationDataManager.INSTANSE.updateRSSI(rssi);
    }

    public void setRtcmThread(boolean bStart) {
        try {
            if (bStart) {
                if (mRTCMThread != null)
                    return;
                mRTCMThread = new RTCMThread();
                mRTCMThread.start();
            } else if (mRTCMThread != null) {
                mRTCMThread.stop();
            }
        } catch (Exception e) {

        }
    }

    @Override
    protected void finalize() {
        stop();
    }

    public void stop() {

        synchronized (PACK_HEAD) {
            PACK_HEAD.notifyAll();
        }

        synchronized (PACK_FLAG) {
            PACK_FLAG.notifyAll();
        }

        if (mNmeaTxThread != null)
            mNmeaTxThread.interrupt();
        if (mNmeaThread != null)
            mNmeaThread.interrupt();
        if (mBLETxDataThread != null)
            mBLETxDataThread.interrupt();
        if (mACKTxThread != null)
            mACKTxThread.interrupt();
    }

    void BLEDealData(CopyOnWriteArrayList<byte[]> List) {
        if (List.size() != 0) {

            for (int nPktCnt = 0; nPktCnt < List.size(); ++nPktCnt) {
                StringBuilder sb = new StringBuilder();
                byte[] tmps = new byte[((byte[]) List.get(nPktCnt)).length - 1];
                System.arraycopy(List.get(nPktCnt), 1, tmps, 0, ((byte[]) List.get(nPktCnt)).length - 1);

                try {
                    sb.append(new String(tmps, "utf-8"));
                } catch (UnsupportedEncodingException var8) {
                    var8.printStackTrace();
                }

                if (this.para == null) {
                    this.para = new StringBuilder();
                }

                for (int i = 0; i < sb.length(); ++i) {
                    if (sb.charAt(i) == 36) {
                        if (this.flag_para_start) {
                            this.flag_para_start = false;
                            this.decodeNMEA(this.para.toString(), 0);
                            this.para.delete(0, this.para.length());
                        }

                        this.flag_para_start = true;
                    }

                    if (this.flag_para_start) {
                        this.para.append(sb.charAt(i));
                    }

                    if (sb.charAt(i) == 10) {
                        this.flag_para_start = false;
                        this.decodeNMEA(this.para.toString(), 0);
                        this.para.delete(0, this.para.length());
                    }
                }
            }

        }
    }

    boolean CheckSum(byte[] buff) {
        if (buff.length <= 0) {
            return false;
        } else {
            byte var7 = buff[1];

            int i;
            for (i = 2; i < buff.length && buff[i] != 42; ++i) {
                var7 ^= buff[i];
            }

            String sCheck = Integer.toHexString(var7).toUpperCase();
            if (i + 4 != buff.length - 1) {
                return false;
            } else {
                if (buff[i] == 42 && buff[i + 3] == 13 && buff[i + 4] == 10) {
                    if (sCheck.length() == 2) {
                        if (sCheck.getBytes()[0] == buff[i + 1] && sCheck.getBytes()[1] == buff[i + 2]) {
                            return true;
                        }
                    } else if (sCheck.getBytes()[0] == buff[i + 2]) {
                        return true;
                    }

                    return false;
                } else {

                    return false;
                }
            }
        }
    }

    private void analyzeGNVER(String nmea) {
        if (this.CheckSum(nmea.getBytes())) {
            int index = 0;
            StringBuffer str = new StringBuffer();

            for (int i = 0; i < nmea.length(); ++i) {
                if (nmea.charAt(i) == 44 | nmea.charAt(i) == 42 | i == nmea.length() - 1) {
                    if (!str.toString().equals("")) {
                        switch (index) {
                            case 1:
                                try {
                                    this.BoardVersion = str.toString();
                                } catch (NumberFormatException var9) {
                                    LOG.E("analyzeGNVER", "Illegal BoardVersion: " + str);
                                }
                                break;
                            case 2:
                                try {
                                    this.BootloaderVersion = str.toString();
                                } catch (NumberFormatException var8) {
                                    LOG.E("analyzeGNVER", "Illegal BootloaderVersion: " + str);
                                }
                                break;
                            case 3:
                                try {
                                    this.BGIVersion = str.toString();
                                } catch (NumberFormatException var7) {
                                    LOG.E("analyzeGNVER", "Illegal BGIVersion: " + str);
                                }
                                break;
                            case 4:
                                try {
                                    this.BGIDateTime = str.toString();
                                } catch (NumberFormatException var6) {
                                    LOG.E("analyzeGNVER", "Illegal BGIDateTime: " + str);
                                }
                        }
                    }

                    ++index;
                    str.delete(0, str.length());
                } else {
                    str.append(nmea.charAt(i));
                }
            }

        }
    }

    public boolean decodeNMEA(String buffer, int sourceID) {
        boolean isASCIIEncoded = false;
        if (!this.flagGNVER && buffer.startsWith("$GNVER")) {
            this.analyzeGNVER(buffer);
            isASCIIEncoded = true;
            if (!this.BoardVersion.equals("")) {
                this.flagGNVER = true;
            }
        }

        if (!this.flagPCMDT && buffer.startsWith("$PCMDT,OK")) {
            this.flagPCMDT = true;
            isASCIIEncoded = true;
        }

        if (buffer.startsWith("$PCMDN,OK")) {
            isASCIIEncoded = true;
        }

        if (buffer.startsWith("$PCMD")) {
            isASCIIEncoded = true;
        }

        if (buffer.startsWith("$POWER")) {
            isASCIIEncoded = true;
            this.devicePower = Integer.parseInt(buffer.substring(7, buffer.length() - 2));
        } else {
            isASCIIEncoded = this.nmeaAnalyzer.analyze(buffer, new NmeaAnalyzer.NmeaCallback() {
                public void onNmeaOneBroadcast(String nmea) {
                    if (!nmea.equals("") && onNmeaListener != null) {
                        onNmeaListener.onNmea(nmea);
                    }

                }

                public void onNmeaUpdate(boolean[] hasGSV) {
                    MyLocation location = new MyLocation();
                    location.setLatitude(RtkService.this.nmeaAnalyzer.getLatitude());
                    location.setLongitude(RtkService.this.nmeaAnalyzer.getLongitude());
                    location.setAltitude(RtkService.this.nmeaAnalyzer.getAltitude());
                    location.setSpeed(RtkService.this.nmeaAnalyzer.getSpeed());
                    location.setBearing(RtkService.this.nmeaAnalyzer.getBearing());
                    location.setMode(RtkService.this.nmeaAnalyzer.getMode());
                    location.setPdop(RtkService.this.nmeaAnalyzer.getPdop());
                    location.setHdop(RtkService.this.nmeaAnalyzer.getHdop());
                    location.setVdop(RtkService.this.nmeaAnalyzer.getVdop());
                    location.setSatInView(RtkService.this.nmeaAnalyzer.getSatellites().size());
                    location.setBaseDelay(RtkService.this.nmeaAnalyzer.getBaseDelay());
                    location.setBaseLine(RtkService.this.nmeaAnalyzer.getBaseLine());
                    location.setBaseGPSN(RtkService.this.nmeaAnalyzer.getBaseGPSN());
                    location.setBaseBDSN(RtkService.this.nmeaAnalyzer.getBaseBDSN());
                    location.setBaseElvGpsSnr(RtkService.this.nmeaAnalyzer.getBaseElvGpsSnr());
                    location.setBaseElvBdsSnr(RtkService.this.nmeaAnalyzer.getBaseElvBdsSnr());
                    location.setRoverGPSN(RtkService.this.nmeaAnalyzer.getRoverGPSN());
                    location.setRoverBDSN(RtkService.this.nmeaAnalyzer.getRoverBDSN());
                    location.setRoverElvGpsSnr(RtkService.this.nmeaAnalyzer.getRoverElvGpsSnr());
                    location.setRoverElvBdsSnr(RtkService.this.nmeaAnalyzer.getRoverElvBdsSnr());
                    location.setRoverUsedGPSN(RtkService.this.nmeaAnalyzer.getRoverUsedGPSN());
                    location.setRoverUsedBDSN(RtkService.this.nmeaAnalyzer.getRoverUsedBDSN());
                    double hepe = RtkService.this.nmeaAnalyzer.getHepe();
                    double wepe = RtkService.this.nmeaAnalyzer.getWepe();
                    if (hepe == -1.0D && wepe == -1.0D) {
                        location.setAccuracy(-1.0D);
                        RtkService.this.sdkAccuracy = -1.0D;
                    } else {
                        double ava = hepe * hepe + wepe * wepe;
                        ava = Math.sqrt(ava);
                        RtkService.this.sdkAccuracy = (double) ((int) (ava * Math.pow(10.0D, 3.0D))) / Math.pow(10.0D, 3.0D);
                        location.setAccuracy(ava);
                        location.setHorizonAccuracy(hepe);
                        location.setVerticalAccuracy(wepe);
                    }

                    RtkService.this.nmeaAnalyzer.resetHWepe();
                    SrcManager.INSTANCE.sendLocation(location, RtkService.this.nmeaAnalyzer.srcId);
                }
            });
        }

        return isASCIIEncoded;
    }

    CopyOnWriteArrayList<CopyOnWriteArrayList<byte[]>> DealNmeaListData(CopyOnWriteArrayList<byte[]> List, boolean bAdd) {
        synchronized ("NMEABUFFER") {
            if (this.ListNmeaBackList == null) {
                this.ListNmeaBackList = new CopyOnWriteArrayList();
            }

            if (bAdd) {
                if (List == null) {
                    return null;
                } else {
                    CopyOnWriteArrayList var9 = new CopyOnWriteArrayList();
                    var9.clear();

                    for (int nCnt = 0; nCnt < List.size(); ++nCnt) {
                        byte[] buffer = this.GetIndexBuffer(List, nCnt);
                        if (buffer != null) {
                            var9.add(buffer);
                        }
                    }

                    this.ListNmeaBackList.add(var9);
                    return null;
                }
            } else {
                int nListListSize = this.ListNmeaBackList.size();
                if (nListListSize == 0) {
                    return null;
                } else {
                    this.ListBackList.clear();
                    this.ListBackList.addAll(this.ListNmeaBackList);
                    this.ListNmeaBackList.clear();
                    return this.ListBackList;
                }
            }
        }
    }

    boolean AckTxRequest(boolean bGet, boolean bAckTxRequest) {
        synchronized ("ACKBUFFER") {
            if (bGet) {
                return this.gl_bAckTxRequest;
            } else {
                this.gl_bAckTxRequest = bAckTxRequest;
                return true;
            }
        }
    }

    boolean AckTxDataRequest(boolean bGet, boolean bAckDataTxRequest) {
        synchronized ("ACK_DataBUFFER") {
            if (bGet) {
                return this.gl_bAckDataTxRequest;
            } else {
                this.gl_bAckDataTxRequest = bAckDataTxRequest;
                return true;
            }
        }
    }

    void BLETx2(byte[] buffer) {
        if (bleManager != null) {
            this.m_RtcmCPktStatistic.CNTTxDataCnt();
            this.m_nmeaCPktStatistic.CNTTxDataCnt();
            bleManager.write(buffer);
            this.m_RtcmCPktStatistic.CNTTxDataFailCnt();
            this.m_nmeaCPktStatistic.CNTTxDataFailCnt();
        }
    }

    byte[] GetBleTxData() {
        synchronized (BLETX_BUFFER_RT) {
            if (this.m_ListBLETxBuffer == null) {
                return null;
            } else {
                int nWrite = this.gl_ListBLETxBufferWrite;
                int nRead = this.gl_ListBLETxBufferRead;
                if (nWrite == nRead) {
                    return null;
                } else if (nRead < nWrite) {
                    ++this.gl_ListBLETxBufferRead;
                    return (byte[]) ((byte[]) this.m_ListBLETxBuffer.get(nRead % BUUFER_LEN_MAX)).clone();
                } else {
                    return null;
                }
            }
        }
    }

    byte[] GetBleTxDataAck() {
        synchronized (BLETX_BUFFER_ACK) {
            if (this.m_ListBLETxBufferAck == null) {
                return null;
            } else {
                int nWrite = this.gl_ListBLETxBufferAckWrite;
                int nRead = this.gl_ListBLETxBufferAckRead;
                if (nWrite == nRead) {
                    return null;
                } else if (nRead < nWrite) {
                    ++this.gl_ListBLETxBufferAckRead;
                    return (byte[]) ((byte[]) this.m_ListBLETxBufferAck.get(nRead % BUUFER_LEN_MAX)).clone();
                } else {
                    return null;
                }
            }
        }
    }

    void BLETxAck(byte[] buffer) {
        synchronized (BLETX_BUFFER_ACK) {
            if (this.m_ListBLETxBufferAck == null) {
                this.m_ListBLETxBufferAck = new ArrayList();

                for (int i = 0; i < BUUFER_LEN_MAX; ++i) {
                    byte[] buf = new byte[1];
                    this.m_ListBLETxBufferAck.add(buf);
                }
            }

            this.m_ListBLETxBufferAck.set(this.gl_ListBLETxBufferAckWrite % BUUFER_LEN_MAX, buffer);
            ++this.gl_ListBLETxBufferAckWrite;
        }
    }

    int BLETx(byte[] buffer) {
        synchronized (BLETX_BUFFER_RT) {
            if (this.m_ListBLETxBuffer == null) {
                this.m_ListBLETxBuffer = new ArrayList();

                for (int i = 0; i < BUUFER_LEN_MAX; ++i) {
                    byte[] buf = new byte[1];
                    this.m_ListBLETxBuffer.add(buf);
                }
            }

            this.m_ListBLETxBuffer.set(this.gl_ListBLETxBufferWrite % BUUFER_LEN_MAX, buffer);
            ++this.gl_ListBLETxBufferWrite;
            return 1;
        }
    }

    boolean InitPkt(int nCnt) {
        this.isHead = false;
        int whileTimes = 0;

        do {
            if (this.isHead) {
                this.isHead = false;
                return true;
            }

            byte[] head = new byte[]{126, 6, 1, (byte) nCnt, 0, 122};
            this.BLETx(head);
            this.m_RtcmCPktStatistic.SetTime(System.currentTimeMillis());
            this.m_RtcmCPktStatistic.CNTINIPKT();
            this.m_RtcmCPktStatistic.SaveStatistic(1);
            if (!this.bRtcmInitAckRec) {
                this.bRtcmInitAckRec = true;
            }

            synchronized (PACK_HEAD) {
                try {
                    PACK_HEAD.wait(50L);
                    ++whileTimes;
                } catch (InterruptedException var10) {
                    var10.printStackTrace();
                }
            }

        } while (whileTimes <= 9);


        return false;
    }

    public String getPCMDT() {
        String UTCTimeBuffer = new String();

        try {
            Calendar e = Calendar.getInstance();
            int zoneOffset = e.get(Calendar.ZONE_OFFSET);
            int dstOffset = e.get(Calendar.DST_OFFSET);
            e.add(Calendar.MILLISECOND, -(zoneOffset + dstOffset));
            int year = e.get(Calendar.YEAR);
            int month = e.get(Calendar.MONTH) + 1;
            int day = e.get(Calendar.DATE);
            int hour = e.get(Calendar.HOUR_OF_DAY);
            int minute = e.get(Calendar.MINUTE);
            int second = e.get(Calendar.SECOND);
            UTCTimeBuffer = "$PCMDT," + String.valueOf(year) + "," + month + "," + day + "," + hour + "," + minute + "," + second + "\r\n";
        } catch (Exception var11) {
            var11.printStackTrace();
        }

        return UTCTimeBuffer;
    }

    void DealData2(byte[] buffer, int frameCnt, int nLen) {
        int mark = 0;
        CopyOnWriteArrayList packCache = new CopyOnWriteArrayList();
        int frameNum = frameCnt;

        byte[] pack = new byte[nLen + 1];
        this.m_RtcmCPktStatistic.CNTPerPKTS();
        this.m_RtcmCPktStatistic.SetPKT(frameCnt);

        for (int timePkt = 0; timePkt < frameNum; ++timePkt) {
            System.arraycopy(buffer, mark, pack, 1, nLen);
            mark += nLen;
            pack[0] = (byte) timePkt;
            packCache.add(timePkt, (byte[]) pack.clone());
            this.BLETx((byte[]) packCache.get(timePkt));
        }

        this.isReturnGot = false;
        synchronized (PACK_FLAG) {
            try {
                PACK_FLAG.wait(500L);
            } catch (InterruptedException var28) {
                var28.printStackTrace();
            }
        }

        if (!this.isReturnGot) {
            this.m_RtcmCPktStatistic.CNTTIMEOUT();
        }


        for (; this.packReturn == 1; this.packReturn = 0) {
            byte[] sRet1 = this.repairIndex;
            int stimePkt1 = this.repairIndex.length;

            for (int var19 = 0; var19 < stimePkt1; ++var19) {
                byte timePkt1 = sRet1[var19];
                this.m_RtcmCPktStatistic.CNTREVRETXPKT();
                if (timePkt1 < packCache.size()) {
                    this.BLETx((byte[]) packCache.get(timePkt1));
                }
            }

            this.isReturnGot = false;
            synchronized (PACK_FLAG) {
                try {
                    PACK_FLAG.wait(300L);
                } catch (InterruptedException var26) {
                    var26.printStackTrace();
                }
            }

            if (!this.isReturnGot) {
                this.packReturn = 0;
                this.m_RtcmCPktStatistic.CNTTIMEOUT2();
            }
        }

        packCache.clear();
        this.isHead = false;
        this.packReturn = -1;
    }

    void TxDifDataV2(byte[] buffer, int nLen) {
        int PktCnt = nLen / 19;
        int nRemain = nLen % 19;
        int mark = 0;
        byte nfMaxCnt = 15;
        int nRemainBytes;
        int nPkt;
        int pack2;
        byte pack;
        int pack1;
        int var15;
        byte[] var16;
        byte[] var17;
        if (PktCnt >= 20) {
            nRemainBytes = PktCnt / 20;
            nPkt = PktCnt % 20;

            for (pack2 = 0; pack2 < nRemainBytes; ++pack2) {
                pack = 20;

                for (pack1 = 0; pack1 < 1; ++pack1) {
                    boolean status = this.InitPkt(pack);
                    if (!status) {
                        return;
                    }
                }

                var17 = new byte[pack * 19];
                System.arraycopy(buffer, mark, var17, 0, pack * 19);
                mark += pack * 19;
                this.DealData2(var17, pack, 19);
            }

            pack2 = nPkt;
            if (nPkt > 0) {
                for (var15 = 0; var15 < 1; ++var15) {
                    this.InitPkt(pack2);
                }

                var16 = new byte[pack2 * 19];
                System.arraycopy(buffer, mark, var16, 0, pack2 * 19);
                mark += pack2 * 19;
                this.DealData2(var16, pack2, 19);
            }
        } else if (PktCnt >= nfMaxCnt) {
            nRemainBytes = PktCnt / nfMaxCnt;
            nPkt = PktCnt % nfMaxCnt;

            for (pack2 = 0; pack2 < nRemainBytes; ++pack2) {
                this.InitPkt(nfMaxCnt);
                var17 = new byte[nfMaxCnt * 19];
                System.arraycopy(buffer, mark, var17, 0, nfMaxCnt * 19);
                mark += nfMaxCnt * 19;
                this.DealData2(var17, nfMaxCnt, 19);
            }

            pack2 = nPkt;
            if (nPkt > 0) {
                for (var15 = 0; var15 < 1; ++var15) {
                    this.InitPkt(pack2);
                }

                var16 = new byte[pack2 * 19];
                System.arraycopy(buffer, mark, var16, 0, pack2 * 19);
                mark += pack2 * 19;
                this.DealData2(var16, pack2, 19);
            }
        } else if (PktCnt >= 10) {
            nRemainBytes = PktCnt / 10;
            nPkt = PktCnt % 10;

            for (pack2 = 0; pack2 < nRemainBytes; ++pack2) {
                pack = 10;

                for (pack1 = 0; pack1 < 1; ++pack1) {
                    this.InitPkt(pack);
                }

                var17 = new byte[pack * 19];
                System.arraycopy(buffer, mark, var17, 0, pack * 19);
                mark += pack * 19;
                this.DealData2(var17, pack, 19);
            }

            pack2 = nPkt;
            if (nPkt > 0) {
                for (var15 = 0; var15 < 1; ++var15) {
                    this.InitPkt(pack2);
                }

                var16 = new byte[pack2 * 19];
                System.arraycopy(buffer, mark, var16, 0, pack2 * 19);
                mark += pack2 * 19;
                this.DealData2(var16, pack2, 19);
            }
        } else {
            nRemainBytes = PktCnt;
            if (PktCnt > 0) {
                for (nPkt = 0; nPkt < 1; ++nPkt) {
                    this.InitPkt(nRemainBytes);
                }

                byte[] var13 = new byte[nRemainBytes * 19];
                System.arraycopy(buffer, mark, var13, 0, nRemainBytes * 19);
                mark += nRemainBytes * 19;
                this.DealData2(var13, nRemainBytes, 19);
            }
        }

        if (nRemain > 0) {
            byte var14 = 1;

            for (pack2 = 0; pack2 < 1; ++pack2) {
                this.InitPkt(var14);
            }

            byte[] var18 = new byte[nRemain];
            System.arraycopy(buffer, mark, var18, 0, nRemain);
            this.DealData2(var18, 1, nRemain);
        }

    }

    public long getSDFreeSize() {
        File path = Environment.getExternalStorageDirectory();
        StatFs sf = new StatFs(path.getPath());
        long blockSize = (long) sf.getBlockSize();
        long freeBlocks = (long) sf.getAvailableBlocks();
        return freeBlocks * blockSize / 1024L / 1024L;
    }


    byte[] GetIndexBuffer(CopyOnWriteArrayList<byte[]> list, int nIdex) {
        for (int nCnt = 0; nCnt < list.size(); ++nCnt) {
            if ((((byte[]) list.get(nCnt))[0] & 255) == nIdex) {
                return (byte[]) list.get(nCnt);
            }
        }

        return null;
    }

    public byte[] SdkData(byte[] buffer, boolean bAdd) {
        synchronized (RTCM_BUFFER) {
            if (this.sRtcmPktBuffer == null) {
                this.sRtcmPktBuffer = new CopyOnWriteArrayList();
            }

            if (bAdd) {
                if (buffer == null) {
                    return null;
                } else {
                    this.sRtcmPktBuffer.add(buffer);
                    return null;
                }
            } else if (this.sRtcmPktBuffer.size() == 0) {
                return null;
            } else {
                int nLen = 0;

                for (int sRtcmPkt = 0; sRtcmPkt < this.sRtcmPktBuffer.size(); ++sRtcmPkt) {
                    nLen += ((byte[]) this.sRtcmPktBuffer.get(sRtcmPkt)).length;
                }

                byte[] var13 = new byte[nLen];
                int nMark = 0;

                int u;
                for (int ns = 0; ns < this.sRtcmPktBuffer.size(); ++ns) {
                    u = ((byte[]) this.sRtcmPktBuffer.get(ns)).length;
                    System.arraycopy(this.sRtcmPktBuffer.get(ns), 0, var13, nMark, u);
                    nMark += u;
                }

                this.TxDifDataV2(var13, var13.length);
                this.sRtcmPktBuffer.clear();
                return null;
            }
        }
    }

    private class ACKTxThread extends Thread {
        public void run() {
            while (!interrupted() && bleManager != null && bleManager.isConnected()) {
                try {
                    Thread.sleep(10L);
                } catch (InterruptedException var22) {
                    var22.printStackTrace();
                }

                if (RtkService.this.AckTxRequest(true, true)) {
                    byte[] timePkt = new byte[]{126, 4, 6, 122};
                    RtkService.this.AckTxRequest(false, false);
                    RtkService.this.m_nmeaCPktStatistic.CNTINIPKTACK();
                    RtkService.this.BLETxAck(timePkt);
                }

                if (RtkService.this.AckTxDataRequest(true, true)) {
                    byte[] var23 = new byte[]{126, 4, 8, 122};
                    RtkService.this.AckTxDataRequest(false, false);
                    RtkService.this.gl_bAckAckRec = false;
                    int nRepertCnt = 1;

                    do {
                        RtkService.this.BLETxAck(var23);
                        RtkService.this.m_nmeaCPktStatistic.CNTRETXACK();

                        try {
                            Thread.sleep(50L);
                        } catch (InterruptedException var21) {
                            var21.printStackTrace();
                        }

                        --nRepertCnt;
                    } while (!RtkService.this.gl_bAckAckRec && nRepertCnt >= 0);

                }
            }

        }
    }


    private class BLETxDataThread extends Thread {
        public void run() {
            label79:
            while (!interrupted() && bleManager != null && bleManager.isConnected()) {
                boolean bData = false;
                byte[] sAck = RtkService.this.GetBleTxDataAck();
                if (sAck != null) {
                    bData = true;
                    RtkService.this.BLETx2(sAck);

                    try {
                        Thread.sleep(10L);
                    } catch (InterruptedException var16) {
                        var16.printStackTrace();
                    }

                }

                while (true) {
                    byte[] var19;
                    do {
                        if (RtkService.this.gl_ListBLETxBufferWrite <= RtkService.this.gl_ListBLETxBufferRead) {
                            if (bData) {
                                continue label79;
                            }

                            try {
                                Thread.sleep(50L);
                            } catch (InterruptedException var13) {
                                var13.printStackTrace();
                            }
                            continue label79;
                        }

                        byte[] var17 = RtkService.this.GetBleTxData();
                        if (var17 != null) {
                            RtkService.this.BLETx2(var17);

                            try {
                                Thread.sleep(10L);
                            } catch (InterruptedException var14) {
                                var14.printStackTrace();
                            }

                            bData = true;
                        }

                        var19 = RtkService.this.GetBleTxDataAck();
                    } while (var19 == null);

                    bData = true;
                    RtkService.this.BLETx2(var19);

                    try {
                        Thread.sleep(10L);
                    } catch (InterruptedException var15) {
                        var15.printStackTrace();
                    }

                }
            }

        }
    }

    private class NmeaThread extends Thread {
        public void run() {
            while (!interrupted() && bleManager != null && bleManager.isConnected()) {
                try {
                    Thread.sleep(50L);
                } catch (InterruptedException var16) {
                    var16.printStackTrace();
                }

                int tmpgl_nPktReadCnt;
                if (RtkService.this.nRepert > 3) {
                    byte[] losstimegap = new byte[]{126, 4, 8, 122};
                    RtkService.this.nRepert = 0;
                    RtkService.this.BLETxAck(losstimegap);
                    RtkService.this.gl_bOnreadpkt = false;
                }

                boolean var18 = false;
                if (!RtkService.this.bisLastPkt) {
                    if (RtkService.this.gl_RecPrePktTime != 0L && RtkService.this.gl_nPktReadCnt != 0 && System.currentTimeMillis() - RtkService.this.gl_RecPrePktTime >= 100L) {
                        RtkService.this.m_nmeaCPktStatistic.CNTTIMEOUT();
                        var18 = false;
                    } else {
                        var18 = true;
                    }
                } else {
                    var18 = false;
                }

                if (!var18) {
                    tmpgl_nPktReadCnt = RtkService.this.gl_nPktReadCnt;
                    int tmpgl_nPktCnt = RtkService.this.gl_nPktCnt;
                    if (tmpgl_nPktReadCnt < tmpgl_nPktCnt && tmpgl_nPktReadCnt != 0) {
                        int[] bListFlag = new int[tmpgl_nPktCnt];

                        int reTxPktBuffer;
                        for (reTxPktBuffer = 0; reTxPktBuffer < tmpgl_nPktCnt; ++reTxPktBuffer) {
                            bListFlag[reTxPktBuffer] = 0;
                        }

                        if (RtkService.this.ListNmeaBack.size() != 0) {
                            label119:
                            for (reTxPktBuffer = 0; reTxPktBuffer < RtkService.this.ListNmeaBack.size(); ++reTxPktBuffer) {
                                if ((((byte[]) RtkService.this.ListNmeaBack.get(reTxPktBuffer))[0] & 255) > tmpgl_nPktCnt - 1) {
                                    int ns = 0;

                                    while (true) {
                                        if (ns >= tmpgl_nPktCnt) {
                                            break label119;
                                        }

                                        bListFlag[ns] = 0;
                                        ++ns;
                                    }
                                }

                                bListFlag[((byte[]) RtkService.this.ListNmeaBack.get(reTxPktBuffer))[0] & 255] = 1;
                            }

                            if (tmpgl_nPktReadCnt != 0 && tmpgl_nPktReadCnt < tmpgl_nPktCnt && tmpgl_nPktCnt != 0 && bListFlag != null) {
                                RtkService.this.nReTxCnt = 0;
                                RtkService.this.reTxN = new byte[tmpgl_nPktCnt];

                                for (reTxPktBuffer = 0; reTxPktBuffer < bListFlag.length; ++reTxPktBuffer) {
                                    if (bListFlag[reTxPktBuffer] == 0) {
                                        RtkService.this.reTxN[RtkService.this.nReTxCnt] = (byte) reTxPktBuffer;
                                        ++RtkService.this.nReTxCnt;
                                    }
                                }

                                if (RtkService.this.nReTxCnt > 15) {
                                    RtkService.this.nReTxCnt = 15;
                                }

                                if (RtkService.this.nReTxCnt > 0) {
                                    RtkService.this.m_nmeaCPktStatistic.SetRETXPKT(RtkService.this.nReTxCnt);
                                    RtkService.this.m_nmeaCPktStatistic.CNTRETXCNT();
                                    byte[] var20 = new byte[RtkService.this.nReTxCnt + 5];
                                    var20[0] = 126;
                                    var20[1] = (byte) (RtkService.this.nReTxCnt + 5 & 255);
                                    var20[2] = 2;
                                    var20[3] = (byte) (RtkService.this.nReTxCnt & 255);
                                    System.arraycopy(RtkService.this.reTxN, 0, var20, 4, RtkService.this.nReTxCnt);
                                    var20[RtkService.this.nReTxCnt + 4] = 122;
                                    ++RtkService.this.nRepert;
                                    RtkService.this.bisLastPkt = false;
                                    RtkService.this.gl_bOnreadpkt = true;

                                    RtkService.this.BLETxAck(var20);

                                    try {
                                        Thread.sleep(100L);
                                    } catch (InterruptedException var15) {
                                    }

                                    RtkService.this.gl_RecPrePktTime = System.currentTimeMillis();
                                }
                            }
                        }
                    }
                }
            }

        }
    }

    private class NmeaTxThread extends Thread {

        public void run() {
            while (!interrupted() && bleManager != null && bleManager.isConnected()) {
                try {
                    Thread.sleep(300L);
                } catch (InterruptedException var4) {
                    var4.printStackTrace();
                }

                CopyOnWriteArrayList var5 = RtkService.this.DealNmeaListData((CopyOnWriteArrayList) ListNmeaBack, false);
                if (var5 != null) {
                    int nListListSize = var5.size();
                    if (nListListSize != 0) {
                        for (int index = 0; index < nListListSize; ++index) {
                            RtkService.this.BLEDealData((CopyOnWriteArrayList) var5.get(index));
                        }
                    }
                }
            }

        }
    }

    private class RTCMThread extends Thread {

        public void run() {
            for (; !interrupted() && bleManager != null && bleManager.isConnected(); RtkService.this.SdkData((byte[]) null, false)) {
                try {
                    Thread.sleep(50L);
                } catch (InterruptedException var2) {
                    return;
                }
            }

        }
    }

    public interface OnNmeaListener {
        void onNmea(String nmea);
    }
}
